# Copyright (c) 2017 Presto Labs Pte. Ltd.
# Author: jaewon

import time

# supported since 3.7
if hasattr(time, 'time_ns'):
  get_timestamp = time.time_ns
else:
  from cffi import FFI

  ffi = FFI()

  ffi.cdef('long get_timestamp();')

  _time = ffi.verify('''
#include <sys/time.h>

long get_timestamp(void) {
  struct timeval tv;
  gettimeofday(&tv, NULL);  // Ignore return code.
  return tv.tv_sec * 1000000000LL + tv.tv_usec * 1000LL;
}
''')

  get_timestamp = _time.get_timestamp
